package com.exigen.performance.test.webapp.services.impl;

import java.util.List;

import org.primefaces.model.chart.PieChartModel;

import com.exigen.performance.test.webapp.dao.DashboardDao;
import com.exigen.performance.test.webapp.dao.ProjectDao;
import com.exigen.performance.test.webapp.domain.DashboardEntity;
import com.exigen.performance.test.webapp.domain.ProjectEntity;
import com.exigen.performance.test.webapp.services.DashboardService;

public class DashboardServicesImpl implements DashboardService {

	// DAO classes
	ProjectDao projectDao;
	DashboardDao dashboardDao;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.exigen.performance.test.webapp.services.DashboardService#
	 * deleteDashboardEntity
	 * (com.exigen.performance.test.webapp.domain.DashboardEntity)
	 */
	public boolean deleteDashboardEntity(DashboardEntity entity) {
		try {
			dashboardDao.delete(entity);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.exigen.performance.test.webapp.services.DashboardService#
	 * getAvailableProjectListForDashbord()
	 */
	public List<ProjectEntity> getAvailableProjectListForDashbord() {
		List<ProjectEntity> result = projectDao.getProjectsNotInDashboard();
		if (result.isEmpty()) {
			List<DashboardEntity> dasboardElem = dashboardDao.findAll();
			if (dasboardElem.isEmpty())
				result = projectDao.findAll();
		}
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.exigen.performance.test.webapp.services.DashboardService#
	 * createNewDashboardElement
	 * (com.exigen.performance.test.webapp.domain.DashboardEntity)
	 */
	public void createNewDashboardElement(DashboardEntity entity) {
		dashboardDao.save(entity);
		this.updateDashboard();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.exigen.performance.test.webapp.services.DashboardService#getDashboardList
	 * ()
	 */
	public List<DashboardEntity> getDashboardList() {
		List<DashboardEntity> result = dashboardDao.findAll();
		for (int i = 0; i < result.size(); i++) {
			DashboardEntity entity;
			entity = result.get(i);
			entity.setProjectName(projectDao.findById(
					result.get(i).getProjectId()).getProjectName());
			entity.setPieChartData(new PieChartModel());
			entity.getPieChartData().set("Not executed",
					entity.getOverallCount() - entity.getExecutedCount());
			entity.getPieChartData().set("Executed", entity.getExecutedCount());

		}
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.exigen.performance.test.webapp.services.DashboardService#updateDashboard
	 * ()
	 */
	public void updateDashboard() {
		dashboardDao.updateDashboard();
	}

	// Getters&Setters

	public ProjectDao getProjectDao() {
		return projectDao;
	}

	public void setProjectDao(ProjectDao projectDao) {
		this.projectDao = projectDao;
	}

	public DashboardDao getDashboardDao() {
		return dashboardDao;
	}

	public void setDashboardDao(DashboardDao dashboardDao) {
		this.dashboardDao = dashboardDao;
	}

}
